#include <iostream>
#include <gflags/gflags.h>
#include "etcd.hpp"
#include <thread>
#include "channel.hpp"
#include "./../../../common/log.hpp"

#include "channel.hpp"
#include <iostream>
#include <brpc/server.h>
#include <butil/logging.h>
#include "main.pb.h"

DEFINE_bool(run_mode, false, "程序运行模式 false-调试 true-发布");
DEFINE_string(log_file, "log.txt", "发布模式下 输出文件的，文件名");
DEFINE_int32(log_level, 0 , "日志等级");


// 1, 继承EchoService创建一个子类，并实现rpc调用的业务功能

class EchoServiceImp1 : public example::EchoService
{
public:
    void Echo(google::protobuf::RpcController* cntl_base,
                const ::example::EchoRequest* request,
                ::example::EchoResponse* response,
                ::google::protobuf::Closure* done) 
    {
        brpc::ClosureGuard rpc_guard(done);
        std::cout << "收到消息" << std::endl;
        std::string str = request->message() + "这是response" ;
        response->set_message(str);
    }
};





const std::string host = "http://127.0.0.1:2379";

int main(int argc, char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);




    // 关闭自带的日志
    logging::LoggingSettings settings;
    settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(settings);

    // 2, 构造服务器对象
    brpc::Server server;

    // 3, 向服务器对象中，新增echoservice服务
    EchoServiceImp1 echo_service;
    server.AddService(&echo_service, brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);

    // 4, 启动服务器
    brpc::ServerOptions options;
    options.idle_timeout_sec = -1;
    options.num_threads = 1;
    int ret = server.Start(7070, &options);
    if(ret == -1)
    {
        std::cout << "服务器启动失败" << std::endl;
        return -1;
    }
    

    // 服务注册
    Registry::ptr  pclient = make_shared<Registry>(host);
    pclient->registry("/service/echo/aaa", "666666666");

    server.RunUntilAskedToQuit(); // 休眠

    std::this_thread::sleep_for(std::chrono::seconds(10));

    return 0;
}